from components.com_sections.models import *
from components.com_categories.models import *
from components.com_users.models import *
from django.db import models

# Create your models here.
class JosContent(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(max_length=255)
    alias = models.CharField(max_length=255)
    title_alias = models.CharField(max_length=255)
    introtext = models.TextField()
    fulltext = models.TextField()
    state = models.IntegerField()
    section = models.ForeignKey(JosSections, db_column='sectionid')
    mask = models.IntegerField()
    category = models.ForeignKey(JosCategories, db_column='catid')
    created = models.DateTimeField()
    # how do we merge jos_users with django users?  is this a migration script?
    # maybe some kind of composite object?
    # joomla stores empty foreign keys as 0's.  need to execute queries to set 
    # foreign keys to defaults if they are assigned a 0 value.
    created_by = models.ForeignKey(JosUsers, db_column='created_by')
    created_by_alias = models.CharField(max_length=255)
    # found cases where modified id 0000-00-00 00:00:00.  need to update database manually to fix this.
    modified = models.DateTimeField()
    # how do we merge jos_users with django users?  is this a migration script?
    # maybe some kind of composite object?
    modified_by = models.ForeignKey(JosUsers, db_column='modified_by')
    checked_out = models.ForeignKey(JosUsers, db_column='checked_out')
    checked_out_time = models.DateTimeField()
    publish_up = models.DateTimeField()
    publish_down = models.DateTimeField()
    images = models.TextField()
    urls = models.TextField()
    attribs = models.TextField()
    version = models.IntegerField()
    # actual content object?
    parentid = models.IntegerField()
    ordering = models.IntegerField()
    metakey = models.TextField()
    metadesc = models.TextField()
    access = models.IntegerField()
    hits = models.IntegerField()
    metadata = models.TextField()

    def __unicode__(self):
        return self.title 
        
    def is_frontpage_content(self):       
        return JosContentFrontpage.objects.filter(content_id__exact=self.id).count() > 0
    is_frontpage_content.boolean=True
        
        
        
    class Meta:
        db_table = u'jos_content'
        verbose_name = u'Content'
        verbose_name_plural = u'Content'
        app_label = u'Content'

class JosContentFrontpage(models.Model):
    content_id = models.IntegerField(primary_key=True)
    ordering = models.IntegerField()
    class Meta:
        db_table = u'jos_content_frontpage'
        verbose_name = u'Front Page Content'
        verbose_name_plural = u'Front Page Content'
        app_label = u'Content'        

class JosContentRating(models.Model):
    content_id = models.IntegerField(primary_key=True)
    rating_sum = models.IntegerField()
    rating_count = models.IntegerField()
    lastip = models.CharField(max_length=50)
    class Meta:
        db_table = u'jos_content_rating'

